import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

/**
 * 图片加载管理类
 */
class ImagesUtil{
  //判断是否是图片
  // static isImg(String url) {
  //   String fileNameSuffix = FileUtils.getFileNameSuffix(filePath: url);
  //   return [
  //     'jpg', 'jpeg','png',
  //     'JPG','JPEG','PNG'
  //   ].contains(fileNameSuffix);
  // }
  static bool isURL(String str, [Map options]) {
    if((str??'').isNotEmpty&&(str.startsWith('http'))){
      return true;
    }
    return false;
  }
  //加载图片
  static Widget loadImage(String imgPath,
      {Key key,BorderRadius radius:BorderRadius.zero,String placeholder : "none",
        double width, double height, BoxFit fit: BoxFit.cover,String package}){
    // TPLogUtils.e('loadImage-->imgPath==$imgPath');
    if((imgPath??'').startsWith('http')){
     return loadNetworkImage(imgPath,key:key,width: width,height: height,radius: radius,fit: fit,placeholder: placeholder);
    }else{
      return loadAssetImage(imgPath,key: key,width: width,height: height,radius: radius,fit: fit,package: package);
    }
  }
  /// 加载本地资源图片
  static Widget loadAssetImage(String imgPath, {Key key,double width, double height,
      BoxFit fit:BoxFit.cover,BorderRadius radius:BorderRadius.zero,Color color,String package}){
    // TPLogUtils.e('loadImage loadAssetImage-->imgPath==$imgPath');
    return ClipRRect(
      key: key,
      borderRadius: radius,
      child: ImagesUtil.loadAssetImageOrigin(
          imgPath,
          width: width,
          height: height,
          fit: fit,
          color: color,
          package: package
      ),
    );
  }
  static Widget loadImageFile(File file,
      {double width, double height, BoxFit fit,double roundRadius:0}){
    return ClipRRect(
      borderRadius: BorderRadius.circular(roundRadius),
      child: ImagesUtil.loadImageFileOrigin(
          file,
          width: width,
          height: height,
          fit: fit,
      ),
    );
  }
  static Widget loadImageFileOrigin(File file,
      {double width, double height, BoxFit fit:BoxFit.cover,double roundRadius:0}){
    Image image = Image.file(file, height: height, width: width, fit: fit,);
    final ImageStream stream = image.image.resolve(ImageConfiguration.empty);
    stream.addListener(ImageStreamListener((ImageInfo image, bool synchronousCall){
    },onError: (dynamic exception, StackTrace stackTrace) {
    } ));
    return image;
  }
  static Widget loadAssetImageOrigin(String imgPath,
      {double width, double height, BoxFit fit,Color color,String package}){
    Image image = Image.asset(

      imgPath??'', height: height, width: width, fit: fit,color: color,package: package,
    );
    final ImageStream stream = image.image.resolve(ImageConfiguration.empty);
    stream.addListener(ImageStreamListener((ImageInfo image, bool synchronousCall){
    },onError: (dynamic exception, StackTrace stackTrace) {
    } ));
    return image;
  }

  /// 加载网络图片
  static Widget loadNetworkImage(String imgPath,
      {Key key,BorderRadius radius:BorderRadius.zero,
    String placeholder : "none", double width, double height, BoxFit fit: BoxFit.cover,String package}){
    // TPLogUtils.e('loadImage loadNetworkImage-->imgPath==$imgPath');
    return Container(
      width: width,
      height: height,
      child: ClipRRect(
        borderRadius: radius,
        child: CachedNetworkImage(
          key: key,
          imageUrl: imgPath??'',
          placeholder: (context, url) => (placeholder=='none')?CupertinoActivityIndicator()
              :loadAssetImage(placeholder, height: height, width: width, fit: fit,package: package),
          errorWidget: (context, url, error) => (placeholder=='none')?Container(child: Icon(Icons.error),):
          loadAssetImage(placeholder, height: height, width: width, fit: fit,package: package),
          width: width,
          height: height,
          fit: fit,
        )
      ),
    );

  }

  /// 加载网络图片
  static Widget FadeInImageAssetNetwork(String imgPath,
      {String placeholder : "none", double width, double height, BoxFit fit: BoxFit.cover}){
    print(imgPath);
    return FadeInImage.assetNetwork(
      image: imgPath == null ? "" : imgPath,
      placeholder:placeholder,
      width: width,
      height: height,
      fit: fit,
    );
  }
  ///图片剪裁
  // static Future<File> cropImage(File imageFile) async {
  //   File file = await ImageCropper.cropImage(
  //       sourcePath: imageFile.path,
  //       aspectRatioPresets: [
  //         CropAspectRatioPreset.square,
  //         CropAspectRatioPreset.ratio3x2,
  //         CropAspectRatioPreset.original,
  //         CropAspectRatioPreset.ratio4x3,
  //         CropAspectRatioPreset.ratio16x9
  //       ],
  //       androidUiSettings: AndroidUiSettings(
  //           toolbarTitle: 'Cropper',
  //           toolbarColor: ColorUtils.color_main,
  //           toolbarWidgetColor: Colors.white,
  //           initAspectRatio: CropAspectRatioPreset.original,
  //           lockAspectRatio: false),
  //       iosUiSettings: IOSUiSettings(
  //         minimumAspectRatio: 1.0,
  //       )
  //   );
  //
  //   print('before zip :${ await file.length()}');
  //   return file;
  // }

}
